package com.ly.lypopupwindowdemo.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lypopupwindowdemo.MessageCenterBean;
import com.ly.lypopupwindowdemo.MessageEdit;
import com.ly.lypopupwindowdemo.OnItemListener;
import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.activity.MessageCenterActivity;
import com.ly.lypopupwindowdemo.adapter.MessageCenterGGAdapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Created by liying on 2017/4/20.
 */

public class MessageCenterGGFragment extends Fragment implements MessageEdit {

    private MessageCenterGGAdapter adapter;
   // @ViewInject(R.id.recycleView)
    private RecyclerView recyclerView;
    private List<MessageCenterBean> lists = new ArrayList<>();

    private HashSet<Integer> positionSet;

    MessageCenterActivity activity;

    private final int CURRENT_SHOW = 1;
    private final int SHOW_EMPTY = 2;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case CURRENT_SHOW:
                    if (adapter != null) {
                        adapter.isEdit(false);
                        adapter.notifyDataSetChanged();
                    }
                    break;
                case SHOW_EMPTY:
//                    if (statusLayout!=null){
//                        statusLayout.showEmpty();
//                    }
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //  initStatusView();
        View view = inflater.inflate(R.layout.fragment_message_center_gg, container, false);
        ViewUtils.inject(getActivity());

        recyclerView = (RecyclerView) view.findViewById(R.id.recycleView);
        positionSet = new HashSet<>();
        initRecycleView(view);

//        if (lists.size()==0){
//            statusLayout.showEmpty();
//        }
        activity = (MessageCenterActivity) getActivity();
        activity.setOnMessageEdit(this);
        return view;

    }

    private void initRecycleView(View view) {

//        for (int i = 0; i < 12; i++) {
//            MessageCenterBean bean = new MessageCenterBean("前方高能0，亿丰金融福利");
//            lists.add(bean);
//        }

        MessageCenterBean bean1 = new MessageCenterBean("前方高能0，亿丰金融福利");
        MessageCenterBean bean2 = new MessageCenterBean("前方高能1，亿丰金融福利");
        MessageCenterBean bean3 = new MessageCenterBean("前方高能2，亿丰金融福利");
        MessageCenterBean bean4 = new MessageCenterBean("前方高能3，亿丰金融福利");
        MessageCenterBean bean5 = new MessageCenterBean("前方高能4，亿丰金融福利");
        MessageCenterBean bean6 = new MessageCenterBean("前方高能5，亿丰金融福利");
        MessageCenterBean bean7 = new MessageCenterBean("前方高能6，亿丰金融福利");
        MessageCenterBean bean8 = new MessageCenterBean("前方高能7，亿丰金融福利");
        MessageCenterBean bean9 = new MessageCenterBean("前方高能8，亿丰金融福利");
        MessageCenterBean bean10 = new MessageCenterBean("前方高能9，亿丰金融福利");
        MessageCenterBean bean11 = new MessageCenterBean("前方高能10，亿丰金融福利");
        MessageCenterBean bean12 = new MessageCenterBean("前方高能11，亿丰金融福利");
        MessageCenterBean bean13 = new MessageCenterBean("前方高能12，亿丰金融福利");
        MessageCenterBean bean14 = new MessageCenterBean("前方高能13，亿丰金融福利");
        MessageCenterBean bean15 = new MessageCenterBean("前方高能14，亿丰金融福利");
        MessageCenterBean bean16 = new MessageCenterBean("前方高能15，亿丰金融福利");

        lists.add(bean1);
        lists.add(bean2);
        lists.add(bean3);
        lists.add(bean4);
        lists.add(bean5);
        lists.add(bean6);
        lists.add(bean7);
        lists.add(bean8);
        lists.add(bean9);
        lists.add(bean10);
        lists.add(bean11);
        lists.add(bean12);
        lists.add(bean13);
        lists.add(bean14);
        lists.add(bean15);
        lists.add(bean16);


        adapter = new MessageCenterGGAdapter(getActivity(), lists, positionSet);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

        adapter.setOnItemListener(new OnItemListener() {
            @Override
            public void checkBoxClick(int position) {
                //已经有Item被选择，执行添加或删除操作
                addOrRemove(position);
            }

            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(activity, "点击第" + position + "个item", Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 操作Item记录集合
     *
     * @param position
     */
    private void addOrRemove(int position) {

        if (positionSet.contains(position)) {
            //如果包含则撤销选择
            Log.e("Message", "remove");
            positionSet.remove(position);
            //lists.get(position).isSelect=false;

        } else {
            //如果不包含，则添加
            Log.e("Message", "add");
            positionSet.add(position);
            // lists.get(position).isSelect=true;
        }
    }


    @Override
    public void onEdit() {

        //   ToastUtil.showShortToast("公告onEdit");

        adapter.isEdit(true);

        adapter.notifyDataSetChanged();

    }

    @Override
    public void onCancleEdit() {
        Toast.makeText(activity, "公告onCancle", Toast.LENGTH_SHORT).show();

        adapter.isEdit(false);
        HashSet<MessageCenterBean> valueSet = new HashSet<>();
        for (Integer integer : positionSet) {
            valueSet.add(adapter.getItem(integer));
        }

        for (MessageCenterBean bean : valueSet) {
            bean.setSelect(false);
        }
        adapter.notifyDataSetChanged();
        positionSet.clear();

    }

    @Override
    public void onCheckAll() {
        //ToastUtil.showShortToast("公告onCheckAll");

        // adapter.setIsAllCheck(1);
        for (int i = 0; i < lists.size(); i++) {
            lists.get(i).setSelect(true);
            // lists.get(i).isSelect = true;
            positionSet.add(i);
        }
        adapter.notifyDataSetChanged();


    }

    @Override
    public void onRead() {
        //  ToastUtil.showShortToast("公告onRead");

        //判断当前是否全选状态
//        if (adapter!=null){
//            if (adapter.isAllCheck()){
//                //全选
//                adapter.setAllRead(true);
//                adapter.notifyDataSetChanged();
//            }
//        }
        HashSet<MessageCenterBean> valueSet = new HashSet<>();
        for (Integer integer : positionSet) {
            valueSet.add(adapter.getItem(integer));
        }

        for (MessageCenterBean bean : valueSet) {
            bean.setRead(true);
            bean.setSelect(false);
        }
        adapter.notifyDataSetChanged();
        positionSet.clear();

    }

    @Override
    public void onDelete() {
        //  ToastUtil.showShortToast("公告onDelete");
        /**
         * 删除
         * 获得positoinSet里面的所有选中的position
         * 根据position得到相应的bean
         * 把bean放到valueSet中。
         * 调用adapter的remove方法把相对应的bean移走
         * 清空positionSet
         */

//        if (positionSet.size() == 0) {
//            //没有选中项
//            ToastUtil.showShortToast("当前没有选中项哦~");
//        }
        HashSet<MessageCenterBean> valueSet = new HashSet<>();
        for (Integer integer : positionSet) {
            valueSet.add(adapter.getItem(integer));
        }

        for (MessageCenterBean bean : valueSet) {
            adapter.remove(bean);
        }
        adapter.notifyDataSetChanged();
        positionSet.clear();

        //如果删除了全部数据，显示emptyView

        if (lists.size() == 0) {
            //statusLayout.showEmpty();

            handler.sendEmptyMessage(SHOW_EMPTY);
        }


    }

    @Override
    public void onCancleCheckAll() {
        //   ToastUtil.showShortToast("取消全选");
        //  adapter.setIsAllCheck(0);
        for (int i = 0; i < lists.size(); i++) {
            lists.get(i).setSelect(false);
            //lists.get(i).isSelect =false;
            positionSet.remove(i);
        }
        adapter.notifyDataSetChanged();
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (isVisibleToUser) {
            //  Log.e("Message","公告页面显示？"+isVisibleToUser);
        } else {
            //  Log.e("Message","公告页面不显示？"+isVisibleToUser);

            handler.sendEmptyMessage(CURRENT_SHOW);

        }


    }

    private void initStatusView() {

//        statusView = MyStatusView.getInstance(mContext, "您还没有公告消息~", new MyStatusView.onRetryClickLister() {
//            @Override
//            public void onRetryClick() {
//
//                ToastUtil.showShortToast("加载数据");
//            }
//        });
//
//        statusLayout =
//                new StatusLayout.Builder().setContentView(recyclerView).setStatusView(statusView).build();
    }

}
